package com.ctrip.study.algorithm.queue;

public class MyArrayPriorityQueue implements MyQueue<Long>{
	
	private int maxSize;
	private Long[] array;
	private int nItem; //当前队列中的数量
	
	public MyArrayPriorityQueue(int maxSize){
		this.maxSize = maxSize;
		this.array = new Long[maxSize];
		nItem = 0;
	}

	@Override
	public boolean isEmpty() {
		return nItem == 0;
	}

	@Override
	public boolean isFull() {
		return nItem == maxSize;
	}

	@Override
	public void insert(Long data) {
		if(nItem == 0){
			array[nItem] = data;
		}else{
			int i;
			for (i = nItem - 1; i >= 0; i--) {
				if(data > array[i]){
					array[i + 1] = array[i];
				}else{
					break;
				}
			}
			array[i + 1] = data;
		}
		nItem++;
	}

	@Override
	public Long remove() {
		Long data = array[nItem - 1];
		nItem--;
		return data;
	}

	@Override
	public int size() {
		return nItem;
	}

}
